package leetcode_2401_2500;

public class LeeCode_2444 {
    public static void main(String[] args) {
        System.out.println(countSubarrays(new int[]{1, 2, 5, 2, 7, 5}, 1, 5));
//        System.out.println(countSubarrays(new int[]{1, 1, 1, 1}, 1, 1));
    }

    private static long countSubarrays(int[] nums, int minK, int maxK) {
        int n = nums.length;
        int start = 0, r1 = start - 1, r2 = start - 1;
        long ans = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] < minK || nums[i] > maxK){
                start = i + 1;
                r1 = start - 1;
                r2 = start - 1;
                continue;
            }
            if (nums[i] == minK){
                r1 = i;
            }
            if (nums[i] == maxK){
                r2 = i;
            }
            ans += Math.min(r1, r2) - start + 1;
        }
        return ans;
    }
}